package com.whf.android.jar.web.downLoad;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.webkit.DownloadListener;

import androidx.core.content.ContextCompat;

import com.whf.android.jar.app.Latte;
import com.whf.android.jar.util.log.LatteLogger;
import com.whf.android.jar.web.activity.WebUiActivity;
import com.whf.android.jar.web.utils.HtmlUtils;


/**
 * 调用其它浏览器下载文件：
 * <p> 如果你手机内存在多个浏览器的话，会打开一个对话框供你选择其中一个浏览器进行下载~
 *
 * @author qf
 * @version 1.0.3
 */
public class DownLoadWebView implements DownloadListener {

    private final Context context;

    public DownLoadWebView(Context context) {
        if (context == null) {
            this.context = Latte.getActivity();
        } else {
            this.context = context;
        }
    }

    @Override
    public void onDownloadStart(String url, String userAgent, String contentDisposition,
                                String mimetype, long contentLength) {
        LatteLogger.i("url=" + url
                + " ;\nuserAgent=" + userAgent
                + " ;\nmimetype=" + mimetype
                + " ;\ncontentLength=" + contentLength);
        if (HtmlUtils.isPdfJs(url)) {
            WebUiActivity.start(context, "", url);
        } else {
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            ContextCompat.startActivity(context, intent, null);
        }
    }
}
